<? // employees - list of employees

// must be admin
if( !$_SESSION['admin'] ) ajax_exit( 'error', "Must be logged in as admin" );

// skills
$skls = db_get_rcds( "select skl_id, skl_cd, active, case when active then name else 'X:'||name end as name from skill order by active desc, skl_cd" );

// build skill cross_ref
$cols = '';
$joins = '';
for( $i=0; $i<count($skls); $i++ ) {
	if( !$skls[$i]['active'] ) continue;
	$alias = "sk{$skls[$i]['skl_id']}";
	$pk = $skls[$i]['skl_id'];
	$cols  .= ", $alias.able as {$alias}_able";
	$joins .= " left join emp_skl $alias on( $alias.emp_id = emp.emp_id and $alias.skl_id=$pk )";
}

// employees
mysql_query( "create or replace view employee_v as select emp.emp_id, emp.emp_cd, name, emp.active, emp.emp_tp $cols from employee emp $joins order by active desc, name" );
$emps =  db_get_rcds( "select * from employee_v" );


ajax_exit( 'success', count($emps).' Employees', Array( 'rcds'=>$emps, 'skls'=>$skls ) );

exit;

?>
